package com.kristin.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

@RestController
public class ExampleController {

    private static final Logger logger = LoggerFactory.getLogger(ExampleController.class);

    private static final AtomicLong num = new AtomicLong(1);

    @Autowired
    public JdbcTemplate jdbcTemplate;

    @RequestMapping("/test")
    public String getTest() {
        String name = "kristin" + num.getAndIncrement();
        logger.info("insert " + name);
        try {
            int i = jdbcTemplate.update("insert into my_entity(name) values(\"" + name + "\") ");
            // 随机返回错误码
            if (i > 0) {
                return "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "exception";
        }
        return "fail";
    }

    @RequestMapping("/test1")
    public String getTest1() throws InterruptedException {
        // 随机返回错误码
        Random random = new Random();
        random.ints(10);
        String code = "success";
        Thread.sleep(300);
        logger.info("code ");
        return code;
    }


}
